<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/include/base.jsp"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en">
<head>
<link rel="shortcut icon" href="img/favicon.png"/>
<script  type="text/javascript"	src="static/js/pageComm.js"></script>


<script type="text/javascript">
		
		$(function(){
			
			$(document).on("click",".updateDContent",function(){
				//清空表单样式及内容
				reset_form("#dictionaryForm");
				//设置模态框标题
				$("#dictionaryLabel").text("字典类型信息修改");
				//设置输入框标题
				 $(".control-label").eq(0).html("字典名称");
				 $(".control-label").eq(1).html("字典类型");
					
				//设置name属性
				 $(".form-control").eq(0).attr("name","dictionaryName");
				 $(".form-control").eq(1).attr("name","typeId");
				 $(".form-control").eq(2).attr("name","dictionaryRemark");

				
				//加载所有的类型信息  放入多选框中
				//加载所有的模块信息  放入多选框中
			  getAllDTypes("${APP_PATH}/dTypes","#dictorySelect select");
				//根据id获取 该字典类型的详细信息 
				getDContentById($(this).attr("dContentId"));
				//打开模态框
				$("#dictionaryModal").modal({
					backdrop: "static"
				});
			});
			
			$("#newDContent").click(function(){
				//清空表单样式及内容
				reset_form("#dictionaryForm");
				//设置模态框标题
				$("#dictionaryLabel").text("新增字典");
				//设置输入框标题
				 $(".control-label").eq(0).html("字典名称");
				 $(".control-label").eq(1).html("字典类型");
				//设置name属性
				 $(".form-control").eq(0).attr("name","dictionaryName");
				 $(".form-control").eq(1).attr("name","typeId");
				 $(".form-control").eq(2).attr("name","dictionaryRemark");
				
					//加载所有的模块信息  放入多选框中
				  getAllDTypes("${APP_PATH}/dTypes","#dictorySelect select");

				//打开模态框
				$("#dictionaryModal").modal({
					backdrop: "static"
				});
			});
			
			
			$("#newDContent").click(function(){
			 	
				//设置模态框标题
				$("#dictionaryLabel").text("新增字典");
				//设置输入框标题
				 $(".control-label").eq(0).html("字典名称");
				 $(".control-label").eq(1).html("字典类型");
				//加载该部门？？

				//打开模态框
				$("#dictionaryModal").modal({
					backdrop: "static"
				});
			});
			
			//新增获取 更新 字典
			$("#saveDictionary").click(function(){
				//alert($("#dictionaryForm").serialize());
				//alert($("#dContentIdHidden").val())
				if(undefined == $("#dContentIdHidden").val() || $("#dContentIdHidden").val()=='' || $("#dContentIdHidden").val() ==null){
				//	alert("新建");
					//新建字典
					newDictionaryOther("${APP_PATH}/dContent","#dictionaryForm","#dictionaryModal");
				}else{
				//	alert("更新");
					//更新字典
					updateDictionOther("${APP_PATH}/dContent","#dictionaryForm","#dictionaryModal");
					
				}
			});
			
			//删除一条记录
			$(document).on("click",".deleteDContent",function(){
				var dContentId=$(this).attr("dContentId");
 				var dictionaryName=$(this).attr("dictionaryName");
 				
 				if(confirm("确认删除 【 "+ dictionaryName+" 】 ？")){
 					$.ajax({
 						url:"${APP_PATH}/dContent/"+dContentId,
 						type:"DELETE",
 						success:function(result){
 							to_page(currentPage);
 						}
 					});
 				}
			})
			
 			//删除多条消息
 			$("#deleteDContentList").click(function(){
 				
 				var checked=$("#dContentTable tbody input:checked");

 				 var dContentIds="";
 				 var dictionaryNames="";
 				 checked.each(function(index,value){
 					dictionaryNames += $(this).attr("dictionaryName")+",";
 					dContentIds += $(this).attr("dContentId")+"-";
 				 });
 				 dictionaryNames=dictionaryNames.substring(0,dictionaryNames.length-1);
 				dContentIds=dContentIds.substring(0,dContentIds.length-1);

 				 if(confirm("确认要删除 【 "+dictionaryNames+" 】 吗？")){
 					 $.ajax({
 						 url:"${APP_PATH}/dContent/"+dContentIds,
 						 type:"DELETE",
 						 success:function(result){
 							 //返回当前页
 							 to_page(currentPage);
 						 }
 					 });
 				 }
 			});
			
			
			//打开第一页
			to_page(1);
			
		});
		//根据id获取 该字典类型的详细信息 
		function getDContentById(dContentId){
			$.ajax({
				url:"${APP_PATH}/dContent/"+dContentId,
				type:"GET",
				success:function(result){
					//将字典信息显示在输入框
					showDContentFromModal(result.extend.dContent);
				}
			});
		}
		//将字典信息显示在输入框
		function showDContentFromModal(dContent){
			$("#dictionaryForm").append($("<input type='hidden' name='id' id='dContentIdHidden'></input>").val(dContent.id));
			$("#dName").val(dContent.dictionaryName);
			//alert(dContent.typeId);
			$("#dictorySelect select").val(dContent.typeId);
			$("#remark").val(dContent.dictionaryRemark);
		}
		
		
		
		function to_page(pn){
		
			$.ajax({
				url:"${APP_PATH}/dContentes/"+pn,
				type:"GET",
				success:function(result){
					
					//2. 解析并显示分页数据
	 				  build_page_info(result);
					
	 				 //3解析显示分页条
	 				  build_page_nav(result);
					
	 				 
	      				//1. 解析并显示员工数据
	 				 build_emps_table(result);
					
				}
			});
		}

		//1. 解析并显示员工数据
			function build_emps_table(result){
				//清空table表格
		  		$("#dContentTable tbody").empty();
				//获取返回的dept值
				var dContent=result.extend.pageInfo.list;
				
                  $.each(dContent,function(index,item){
						var checkboxTd=$("<td></td>").append($("<input></input>")
								.addClass("checkbox")
								.attr("dictionaryName",item.dictionaryName)
								.attr("dContentId",item.id)
								.attr( "type","checkbox")).attr("align","center");
						
						var idTd=$("<td></td>").append(item.id);
						var dictionaryNameTd=$("<td></td>").append(item.dictionaryName);
						
						if(item.dType ==null || item.dType == ""){
							var dTypeTd=$("<td></td>");
						}else{
							var dTypeTd=$("<td></td>").append(item.dType.dTypeName)
						}
						
						var editBtn=$("<a></a>").addClass("btn btn-primary updateDContent").append("修改");
						var deleteBtn=$("<a class='btn btn-danger deleteDContent' >删除</a>")
						editBtn.attr("dContentId",item.id);
						deleteBtn.attr("dContentId",item.id);
						deleteBtn.attr("dictionaryName",item.dictionaryName);
						var btnGroupTd=$("<td style='text-align:center;'></td>").append($("<div></div>").addClass("btn-group"));
						
						btnGroupTd.append(editBtn).append(deleteBtn);
						
						$("<tr></tr>").append(checkboxTd)
									  .append(idTd)
									  .append(dictionaryNameTd)
									  .append(dTypeTd)
									  .append(btnGroupTd)
									  .appendTo($("#dContentTable tbody"));
						
					});
		  	}
		
		
		</script>
</head>


  <body>
  <!-- 模态框 -->
  <%@include file="/WEB-INF/include/dictionaryModal.jsp" %>
  
  <!-- container section start -->
  <section id="container" class="">
      <%@include file="/WEB-INF/include/top.jsp"%>

      <%@include file="/WEB-INF/include/left.jsp"%>

      <!--main content start-->
      <section id="main-content" >
          <section class="wrapper">
		  <div class="row">
				<div class="col-lg-12">
					<ol class="breadcrumb">
						<li><i class="fa fa-home"></i><a href="index.jsp">字典管理</a></li>
						<li><i class="fa fa-table"></i>字典内容</li>
					</ol>
					
				</div>
			</div>
	          
             
            
            
              <div class="row" align="center" style="margin:0 auto">
                  <div class="col-lg-12">
                      <section class="panel">
                      
                      <table class="table table-striped table-advance table-hover" id="dContentTable">
                          <thead>
                          	  <tr>
                                 <th width="8%" style="text-align:center;"><input type="checkbox" id="checkAll" /></th>
                                 <th width="13%">ID</th>
                                 <th width="37%">字典名称</th>
                                 <th width="20%">字典类型</th>
                                 <th width="22%" style="text-align: center;"><span>操作&nbsp;&nbsp;&nbsp;</span>
                                 	<span class="btn-group" >
                                      <a class="btn btn-primary" id="newDContent" >新增</a>
                                      <a class="btn btn-danger" id="deleteDContentList">删除</a>
                      				</span>
                                 </th>
                              </tr>
                          </thead>
                           <tbody></tbody>
                           <tfoot>
                           <tr><td colspan="5">
                           
                    <div class="row " >
			<!--分页文字信息  -->
			<div class="col-md-6" id="page_info_area"></div>
			<!-- 分页条信息 -->
			<div class="col-md-6" id="page_nav_area" ></div>
		</div>       
                           
                           </td></tr>
        
                           </tfoot>
                        </table>
                        
                          
                      </section>
                  </div>
              </div>
         </section>
      </section>
      <!--main content end-->
  </section>
  <!-- container section end -->
    <!-- javascripts -->
    <script src="js/jquery.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <!-- nicescroll -->
    <script src="js/jquery.scrollTo.min.js"></script>
    <script src="js/jquery.nicescroll.js" type="text/javascript"></script>
    <!--custome script for all page-->
    <script src="js/scripts.js"></script>


  </body>
</html>
